﻿using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestEntityFramework;
using TestEntityFramework.Entities;
using TestEntityFramework.Interfaces;
using ConsoleApplication1;
using System.Windows.Forms;
using TestEntityFramework.Model;
using Common;

namespace IngresoHoras
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DependencyFactory.Container.RegisterType<IUnitTransaction, UnitTransaction>();
            using (IUnitTransaction uow = DependencyFactory.CreateUnitOfWork())
            {
                cmbTareas.Items.Add("Seleccionar tarea");
                foreach(var item in uow.DAOTareas.GetTareas())
                {
                    cmbTareas.Items.Add(item.IdTarea + " - " + item.Nombre);
                }
                cmbTareas.SelectedIndex = 0;
            }
        }

        private void cmbTareas_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbSubTareas.Items.Clear();
            cmbSubTareas.Items.Add("Seleccionar sub tarea");
            cmbSubTareas.SelectedIndex = 0;
            using (IUnitTransaction uow = DependencyFactory.CreateUnitOfWork())
            {
                if (cmbTareas.SelectedIndex != 0)
                {
                    string[] idTarea = cmbTareas.SelectedItem.ToString().Split('-');
                    foreach (var item in uow.DAOSubTareas.GetSubTareasPorTarea(int.Parse(idTarea[0])))
                    {
                        cmbSubTareas.Items.Add(item.IdSubTarea + " - " + item.Nombre);
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (IUnitTransaction uow = DependencyFactory.CreateUnitOfWork())
            {
                string[] idTarea = cmbTareas.SelectedItem.ToString().Split('-');
                string[] idSubTarea = cmbSubTareas.SelectedItem.ToString().Split('-');
                IngresoHora tarea = new IngresoHora();
                tarea.NombreUsuario = ConfigurationManager.AppSettings["Usuario"];
                tarea.Horas = int.Parse(txtHoras.Text);
                tarea.IdTarea = int.Parse(idTarea[0]);
                tarea.IdSubTarea = int.Parse(idSubTarea[0]);
                tarea.Fecha = dateTimePicker1.Value.Date;
                uow.DAOIngresoHoras.insert(tarea);
                uow.Save();

                cmbTareas.SelectedIndex = 0;
                cmbSubTareas.SelectedIndex = 0;
                txtHoras.Text = string.Empty;

            }
        }
    }
}
